package com.zyl.points;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import com.zyl.points.bean.Question;
import com.zyl.points.util.DBHelper;
import com.zyl.points.util.FileUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.HeaderViewListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class FileExplorer extends Activity {
	
    private ListView lvList;
    
    //当前目录
    private File currDir ;

	//private Button btnFooter;

	private Button btnHeader;
	
	//最后选择的文件(也可以选择目录)
	private File lastFile ;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.felist);
        
        Resources r = this.getResources();
        
        lvList = (ListView) findViewById(R.id.lvList);
        
        //长按事件
        lvList.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
				return false;
			}
		});
        
        //通过资源对象加载Drawable
     //   Drawable greenLine = r.getDrawable( );
     //   lvList.setDivider(greenLine);
        lvList.setDividerHeight(1);
        
        /****************************** 设置Header和Footer **********************************/
        View.OnClickListener ll = new View.OnClickListener() {
			public void onClick(View v) {
				//取出目录的上级目录
				File pf = currDir.getParentFile();
				//判断是否是根目录
				if(pf != null){
					currDir = pf ;
					lvList.setAdapter(new MyAdpater(currDir));
					setHeaderNFooterText();
				}
			}
		};
		
        btnHeader = new Button(this);
        btnHeader.setText("向上");
        //取得向上图标
        Drawable upIcon = r.getDrawable(R.drawable.upload_48);
        //设置图标的边界
       
        upIcon.setBounds(0, 0, 35, 35);
        //设置左侧图标
        btnHeader.setCompoundDrawables(upIcon, null, null, null);
        //设置文本中心
        btnHeader.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
        btnHeader.setOnClickListener(ll);
        lvList.addHeaderView(btnHeader);
        
       // btnFooter = new Button(this);
       // btnFooter.setText("向上");
        //设置左侧图标
      //  btnFooter.setCompoundDrawables(upIcon, null, null, null);
        //设置文本中心
      //  btnFooter.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
      //  btnHeader.setOnClickListener(ll);
      //  lvList.addFooterView(btnFooter);
        
        /************************************************************************************/
        
        //设置适配器
        currDir = new File("/") ;
        
        //设置初始目录
        lvList.setAdapter(new MyAdpater(currDir));
        //刷新页面页脚
        setHeaderNFooterText();
        
        lvList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				HeaderViewListAdapter a = (HeaderViewListAdapter) lvList.getAdapter();
				File file = (File) a.getItem(position);
				//是目录
				if(file != null && file.isDirectory()){
					currDir = file ;
					lvList.setAdapter(new MyAdpater(file));
					setHeaderNFooterText();
				}
				//是文件
				else if(file != null){
					setHeaderNFooterText(file.getAbsolutePath());
				}
			}
		});
    }
	
	/**
	 * 设置Header和Footer文本标签
	 */
	public void setHeaderNFooterText(){
		String path = currDir.getAbsolutePath();
		btnHeader.setText(path.substring(1));
	//	btnFooter.setText(path);
	}
	
	/**
	 * 设置Header和Footer文本标签
	 */
	public void setHeaderNFooterText(String path){
		btnHeader.setText(path);
	//	btnFooter.setText(path);
	}
	
	/**
	 * 自定义适配器
	 */
	class MyAdpater extends BaseAdapter{
		
		public File file ;
		
		public File[] children ;
		
		public MyAdpater(File file){
			this.file = file ;
			children = file.listFiles();
		}
		
		public int getCount() {
			if(children != null && children.length > 0){
				return children.length ;
			}
			return 0;
		}

		public File getItem(int position) {
			return children[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView, ViewGroup parent) {
			//初始化布局填充器
			LayoutInflater li = LayoutInflater.from(FileExplorer.this);
			//填充布局文件,返回根对象
			//如果指定root,则整个视图会放置到root中,并返回root,
			LinearLayout llRoot = (LinearLayout) li.inflate(R.layout.feitem, null);
			
			//设置图标
			ImageView iv = (ImageView) llRoot.findViewById(R.id.ivIcon);
			//是否目录
			if(children[position].isDirectory()){
				iv.setImageResource(R.drawable.folder1_48);
				llRoot.findViewById(R.id.tvIcon2).setVisibility(View.GONE);
				llRoot.findViewById(R.id.ivDelete).setVisibility(View.GONE);
			}
			//是文件
			else{
				if(!children[position].getName().toUpperCase().endsWith(".CSV")){
				llRoot.findViewById(R.id.ivDelete).setVisibility(View.GONE);
				}
				
				iv.setImageResource(R.drawable.file_48);
				ImageView ivDelete = (ImageView) llRoot.findViewById(R.id.ivDelete);
				ivDelete.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						//上传监听
						DialogInterface.OnClickListener l = new DialogInterface.OnClickListener(){
							public void onClick(DialogInterface dialog,int which) {
								//上传文件
								 
								FileUtil.upload(position,children,FileExplorer.this);  
						       	
							}

							};
							
						new AlertDialog.Builder(FileExplorer.this)
									.setTitle("上传")
									.setMessage("确认上传该文件?")
									.setPositiveButton("确定", l)
									.setNegativeButton("取消", null)
									.show();
						
					}
				});
			}
			
			//设置文件名称
			TextView tv = (TextView) llRoot.findViewById(R.id.tvFileName);
			tv.setText(children[position].getName());
			return llRoot;
		}
	}
	
	//
	public void click(View v){
		String path = btnHeader.getText().toString();
		Intent i = new Intent();
		i.putExtra("path", path);
		setResult(1000, i);
		this.finish();
	}
	
}